{extend name="base"}
{block name="title"}GO游戏开服{/block}
{block name="head"}
<style>
.border-table{font-size:12px; color:#666;}
</style>
{/block}
{block name="body"}
<body class="mainbody">
<form>
<div class="location">
  <a href="javascript:history.back(-1);" class="back"><i class="iconfont icon-up"></i><span>返回上一页</span></a>
  <a href="{:url('index/home')}" class="home"><i class="iconfont icon-home"></i><span>首页</span></a>
  <i class="arrow iconfont icon-arrow-right"></i>
  <span>GO游戏开服</span>
</div>

<!--工具栏-->
<div id="floatHead" class="toolbar-wrap">
  <div class="toolbar">
    <div class="box-wrap">
      <a class="menu-btn"><i class="iconfont icon-more"></i></a>
      <div class="l-list">
        <div class="menu-list">
          <div class="rule-single-select">
            <select name="channel" style="display:none">
              <option value="-1">===所有渠道===</option>
              {volist name="CHANNELS" id="val"}
              <option value="{$key}"{eq name="channel" value="$key"} selected{/eq}>[{$key}]{$val.Name}</option>
              {/volist}
            </select>
          </div>
          <div class="rule-single-select">
            <select name="kind" style="display:none">
              <option value="-1">===所有游戏===</option>
              {volist name="GAME_KIND" id="v"}
              <option value="{$key}"{eq name="kind" value="$key"} selected{/eq}>[{$key}]{$v}</option>
              {/volist}
            </select>
          </div>
          <div class="rule-single-select">
            <select name="type" style="display:none">
              <option value="-1">===所有类型===</option>
              {volist name="GO_SERVER_TYPE" id="v"}
              <option value="{$key}"{eq name="type" value="$key"} selected{/eq}>[{$key}]{$v}</option>
              {/volist}
            </select>
          </div>
          <div class="rule-single-select">
            <select name="status" style="display:none">
              <option value="-1">===所有状态===</option>
              {volist name="GO_SERVER_STATUS" id="v"}
              <option value="{$key}"{eq name="status" value="$key"} selected{/eq}>[{$key}]{$v}</option>
              {/volist}
            </select>
          </div>
        </div>
      </div>
      <div class="r-list">
        <input type="button" value="创建新服" class="btnNew" />
      </div>
    </div>
  </div>
</div>
<!--/工具栏-->

<div class="table-container">
  <table class="ltable border-table">
    <thead>
      <tr>
        <th>ID</th>
        <th>渠道标识</th>
        <th>所属游戏</th>
        <th>服务名称</th>
        <th>游戏类型</th>
        <th>运行状态</th>
        <th>创建时间</th>
        <th>更新时间</th>
        <th>运行时长</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
{empty name="list"}
      <tr><td colspan="10">Oh~暂无数据!</td></tr>
{else/}
    {volist name="list" id="v"}
    <tr data-id="{$v.ID}" data-st="{$v.Status}">
      <td>{$v.ID}</td>
      <td title="{$v.Channel}">{$CHANNELS[$v.Channel]['Name'] ?? '--'}</td>
      <td>[{$v.KindID}]{$GAME_KIND[$v.KindID] ?? '--'}</td>
      <td>{$v.Title}</td>
      <td class="gst_{$v.Type}">[{$v.Type}]{$GO_SERVER_TYPE[$v.Type] ?? '--'}</td>
      <td class="gss_{$v.Status}">[{$v.Status}]{$GO_SERVER_STATUS[$v.Status] ?? '--'}</td>
      <td>{$v.CreateTime}</td>
      <td>{$v.UpdateTime}</td>
      <td>{gt name="v.RunTime" value="0"}{php}$eax = floor($v['RunTime'] / 24); printf('%02d天%02d时', $eax, $v['RunTime'] - $eax * 24);{/php}{/gt}</td>
      <td><input type="button" value="详情" class="btnDetail" /></td>
    </tr>
    {/volist}
{/empty}
    </tbody>
  </table>
</div>

<div class="page-footer"><div class="btn-wrap">{$list->render()}</div></div>

<script>
$(function() {

  $.extend({
    ServerControl: {
      ShowDetailDialog: function (id, state) {
        var url = "{:url('detail')}?id=" + id;
        var ext = {};

        if (id > 0) {
          ext.button = [];
          ext.button.push({
            value: '删除服务',
            callback: function () {
              $.ServerControl.ShowDeleteConfirm(id);
              return false;
            }
          });
          // Status 0=未运行(启动); 1=开启中(启动); 2=运行中(关闭); 3=关闭中(关闭);
          ext.button.push({
            value: ((state == 0 || state == 1) ? '启动' : '关闭') + '服务',
            callback: function () {
              $.ServerControl.ShowSwitchConfirm(id, state);
              return false;
            }
          });
        }
        
        var arg = $.extend({
          title: '服务详情[' + id + ']',
          url: url,
          width: 530,
          height: 370,
          padding: 0,
          okValue: '确认提交',
          ok: function () {
            var f = $('iframe', this.node);
            var v = $('form', f.contents()).serialize();
            console.log(v);
            $.post(url, v, function (resp, state, xhr) {
              console.log(resp);
              alert(resp.error);
              if (resp.errno == 0) {
                location.reload();
              }
            });
            return false;
          }
        }, ext);
        dialog(arg).showModal();
      },
      ShowDeleteConfirm: function (id) {
        var url = "{:url('delete')}?id=" + id;
        dialog({
          title: '提示',
          content: '确定删除?ServerID=' + id,
          width: 200,
          height: 30,
          okValue: '确认',
          ok: function () {
            $.post(url, function (resp, state, xhr) {
              console.log(resp);
              alert(resp.error);
              if (resp.errno == 0) {
                location.reload();
              }
            });
            return false;
          }
        }).showModal();
      },
      ShowSwitchConfirm: function (id, state) {
        // Status 0=未运行(启动); 1=开启中(启动); 2=运行中(关闭); 3=关闭中(关闭);
        var url = "{:url('switch')}?id=" + id + '&state=' + state;
        dialog({
          title: '提示',
          content: '确定' + ((state == 0 || state == 1) ? '启动' : '关闭') + '?ServerID=' + id,
          width: 200,
          height: 30,
          okValue: '确认',
          ok: function () {
            $.post(url, function (resp, state, xhr) {
              console.log(resp);
              alert(resp.error);
              if (resp.errno == 0) {
                location.reload();
              }
            });
            return false;
          }
        }).showModal();
      }
    }
  });

  $('input.btnNew').click(function () {
    $.ServerControl.ShowDetailDialog(0, 0);
  });
  
  $('input.btnDetail').click(function () {
    var tr = $(this).parents('tr');
    var id = tr.data('id');
    var st = tr.data('st');
    $.ServerControl.ShowDetailDialog(id, st);
  });
  
});
</script>
</form>
</body>
{/block}